﻿/* Copyright (c) 2015 Slava Naumenko - http://m4w.codeplex.com */

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowServices;
using System;
using System.Collections;
using System.Collections.Generic;

namespace M4W.TJ.RunSiteWorkflows
{
    class RunSiteWorkflowsTJ : SPJobDefinition
    {
        private const string JobTitle = "M4W - Run Site Workflows Job";

        public RunSiteWorkflowsTJ()
            : base()
        {

        }

        public RunSiteWorkflowsTJ(string jobName, SPWebApplication webApplication)
            : base(jobName, webApplication, null, SPJobLockType.Job)
        {
            this.Title = JobTitle;
        }

        public override void Execute(Guid targetInstanceId)
        {
            SPWebApplication webApplication = this.Parent as SPWebApplication;

            foreach (SPContentDatabase contentDB in (IEnumerable)webApplication.ContentDatabases)
            {
                if (contentDB != null)
                {
                    foreach (SPSite site in (IEnumerable)contentDB.Sites)
                    {
                        if (site != null)
                        {
                            if (IsFeatureActivated(site))
                            {
                                foreach (SPWeb web in (IEnumerable)site.AllWebs)
                                {
                                    if (web != null)
                                    {
                                        // 2010

                                        foreach (SPWorkflowAssociation association in web.WorkflowAssociations)
                                        {
                                            try
                                            {
                                                if (association.RunningInstances == 0)
                                                {
                                                    site.WorkflowManager.StartWorkflow(null, association, string.Empty, SPWorkflowRunOptions.Asynchronous);
                                                }
                                            }
                                            catch (Exception) { }
                                        }

                                        // 2013

                                        WorkflowServicesManager workflowServicesManager = new Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager(web);
                                        WorkflowInstanceService workflowInstanceService = workflowServicesManager.GetWorkflowInstanceService();

                                        foreach (WorkflowSubscription subscription in workflowServicesManager.GetWorkflowSubscriptionService().EnumerateSubscriptions())
                                        {
                                            try
                                            {
                                                if (workflowInstanceService.CountInstancesWithStatus(subscription, WorkflowStatus.Started) == 0)
                                                {
                                                    workflowInstanceService.StartWorkflow(subscription, new Dictionary<string, object>());
                                                }
                                            }
                                            catch (Exception) { }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        private bool IsFeatureActivated(SPSite site)
        {
            bool isfa = false;

            try
            {
                Guid featureId = new Guid("c3dfa07f-7117-40e2-835a-c133b4dda1e0");
                if (site.Features[featureId] != null) { isfa = true; }
            }
            catch (Exception) { }

            return isfa;
        }
    }
}